package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.Absence;
import com.fs.cv.pojo.pojodao.Student;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.StudentClassDepartment;
import com.fs.cv.pojo.pojoview.StudentInfo;
import com.fs.cv.service.DepartmentService;
import com.fs.cv.service.StudentInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: hj
 * @createDate: 2020/9/30
 * @version: 1.0
 */
@Controller
@RequestMapping("/departmentController")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @RequestMapping("/todepartment")
    public String getView(){
        return "departmentInfo";
    }
    //分页显示
    @RequestMapping("/departmentByPage")
    @ResponseBody
    public Map<String,Object> getList(Integer currentPage,Integer pageSize){
        //获取分页后的查询结果
        PageInfo<StudentClassDepartment> departmentList = departmentService.getByPage(currentPage, pageSize);
        Map<String,Object>result=new HashMap<>();
        result.put("code",0);
        result.put("departmentList",departmentList.getList());//返回查询的分页数据
        result.put("totalPage",departmentList.getPages());//返回当前总页数
        return result;

    }
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String,Object> getOne(int studentId){
        Map<String,Object> result=new HashMap<>();
        //根据学生的id查询出学生的基本信息
        StudentInfo student = departmentService.getById(studentId);
       if (student==null){
           result.put("code",-1);
           result.put("message","无此学生");
       } else {
           result.put("code",0);
           result.put("student",student);
       }
        System.out.println(result);
        return result;
    }
    @RequestMapping("/updateok")
    @ResponseBody
    public Map<String,Object>updateByClassId(String className,int studentId){
        Map<String,Object>result=new HashMap<>();
       System.out.println(className);
       System.out.println(studentId);
       //根据班级姓名查询班级ID
        int classId = departmentService.getClassIdByClassName(className);
        int i = departmentService.updateByClassId(classId, studentId);
        if (i>0){
            result.put("code",0);
        }else {
            result.put("code",-1);
            result.put("massage","修改失败");
        }
        return result;
    }
    //根据姓名进行模糊查询
    @RequestMapping("/seletedByName")
    @ResponseBody
    public Map<String,Object> seletedByName(String studentName){
        Map<String, Object> result = new HashMap<>();
       Set<StudentClassDepartment> student = departmentService.getByName(studentName);
        System.out.println(student);
        if (student!=null && !student.isEmpty()){
            result.put("code",0);
            result.put("student",student);
        }else {
            result.put("code",-1);
            result.put("message","本专业无此人");
        }
        return result;

    }
}
